
const Router = require('koa-router')

const { auth } = require('../middleware/auth.middleware')
const {
 validator
} = require('../middleware/address.middleware')
const {
 create, findAll,
 update, remove,
 setDefault
} = require('../controller/address.controller')

const router = new Router({ prefix: '/address' })

// 添加地址
router.post('/', auth, validator({
 consignee: 'string',
 phone: { type: 'string', format: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/ },
 address: 'string'
}), create)
// 获取地址列表
router.get('/', auth, findAll)
// 修改地址
router.put('/:id', auth, validator({
 consignee: 'string',
 phone: { type: 'string', format: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/ },
 address: 'string'
}), update)
// 删除地址
router.delete('/:id', auth, remove)
// 设置默认地址
router.patch('/:id', auth, setDefault)

module.exports = router
